package com.axjy.web;

import com.axjy.mapper.LogsMapper;
import com.axjy.pojo.Logs;
import com.axjy.pojo.User;
import com.axjy.service.LogsService;
import com.axjy.service.UserService;
import com.axjy.service.vo.Pager;
import com.axjy.util.Log;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by asus on 2018/1/30.
 */
@RestController
@RequestMapping("/sys/user")
public class UserHandler {

    @Resource
    private UserService userService;

    @Resource
    private LogsService logsService;

    @GetMapping("/search")
    public Pager search(@RequestParam(required = false,defaultValue = "0")Integer offset, @RequestParam(required = false,defaultValue = "4")Integer limit, String userName, Integer userStatus, HttpServletRequest request){
        Pager pager=userService.searchPager(offset, limit, userName, userStatus);
        request.setAttribute("userName",userName);
        request.setAttribute("userStatus",userStatus);
        return pager;
    }

    @PostMapping("/add")
    public String add(User user,HttpSession session,Logs logs){
        String msg=null;
        User userCode = (User) session.getAttribute("loginUser");
        try{
            userService.addUser(user);
            msg="新增成功";
        }catch (RuntimeException ex){
            msg="新增失败";
            Log.logger.error(msg,ex);
        }
        logs.setOperateinfo("新增用户："+msg);
        logs.setUsercode(userCode.getUserCode());
        logs.setUsername(userCode.getUserName());
        logsService.addLogs(logs);
        return msg;
    }

    @PostMapping("/update")
    public String update(User user,HttpSession session,Logs logs){
        String msg=null;
        User userCode = (User) session.getAttribute("loginUser");
        try{
            userService.updateUser(user);
            msg="修改成功";
        }catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
        logs.setOperateinfo("修改用户："+msg);
        logs.setUsercode(userCode.getUserCode());
        logs.setUsername(userCode.getUserName());
        logsService.addLogs(logs);
        return msg;
    }

    @PostMapping("/updateStatus")
    public String updateStatus(Integer id,Integer userStatus,HttpSession session,Logs logs){
        String msg=null;
        User userCode = (User) session.getAttribute("loginUser");
        try{
            userService.updateStatus(id, userStatus);
            msg="修改成功";
        }catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
        logs.setOperateinfo("修改用户状态："+msg);
        logs.setUsercode(userCode.getUserCode());
        logs.setUsername(userCode.getUserName());
        logsService.addLogs(logs);
        return msg;
    }

    @PostMapping("/editUser")
    public String editUser(User user, HttpSession session,Logs logs){
        String msg=null;
        User userCode = (User) session.getAttribute("loginUser");
        try{
            userService.editUser(user);
            msg="修改成功";
        } catch (RuntimeException ex){
            msg="修改失败";
            Log.logger.error(msg,ex);
        }
            logs.setOperateinfo("修改用户："+msg);
            logs.setUsercode(userCode.getUserCode());
            logs.setUsername(userCode.getUserName());
            logsService.addLogs(logs);

        return msg;
    }

    @PostMapping("/grant")
    public String grant(Integer userId,Integer roleId, HttpSession session,Logs logs){
        String msg=null;
        User userCode = (User) session.getAttribute("loginUser");
        try {
            userService.updateByRoleId(userId, roleId);
            msg="用户授权成功";
        }catch(RuntimeException ex){
            msg="用户授权失败";
            Log.logger.error(msg,ex);
        }
        logs.setOperateinfo("用户授权："+msg);
        logs.setUsercode(userCode.getUserCode());
        logs.setUsername(userCode.getUserName());
        logsService.addLogs(logs);
        return msg;
    }

}
